package com.example.controller;

import com.example.common.AuthAccess;
import com.example.common.Result;
import com.example.pojo.entity.User;
import com.example.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    /*
    * 新增用户
    * */
    @PostMapping("/add")
    public Result add(@RequestBody User user){
        userService.insert(user);
        return Result.success();
    }

    /*
     * 修改用户
     * */
    @PutMapping("/update")
    public Result update(@RequestBody User user){
        userService.update(user);
        return Result.success();
    }

    /*
     * 删除用户
     * */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        userService.delete(id);
        return Result.success();
    }

    /*
     * 批量删除用户
     * */
    @DeleteMapping("/delete/batch")
    public Result batchdelete(@RequestBody List<Integer> ids){
        userService.batchdelete(ids);
        return Result.success();
    }

    /*
     * 查询所有用户
     * */
    @GetMapping("/selectAll")
    public Result selectAll(){
        List<User> users = userService.selectAll();
        return Result.success(users);
    }

    /*
     * 根据id查询用户
     * */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        User user = userService.selectById(id);
        return Result.success(user);
    }

    /*
     * 根据姓名查询用户
     * */
    @GetMapping("/selectByName")
    public Result selectByName(String name){
        List<User> users = userService.selectByName(name);
        return Result.success(users);
    }

    /*
     * 多条件查询用户
     * */
    @GetMapping("/selectByMore")
    public Result selectByMore(String name,String username){
        List<User> users = userService.selectMore(name,username);
        return Result.success(users);
    }

    /*
     * 多条件模糊查询用户
     * */
    @GetMapping("/selectByLike")
    public Result selectByLike(String name,String username){
        List<User> users = userService.selectByLike(name,username);
        return Result.success(users);
    }

    /*
    * 分页查询
    * */
    @GetMapping("/selectByPage")
    public Result selectByPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "") String name,
            @RequestParam(defaultValue = "") String username){
        PageInfo<User> pageInfo = userService.selectByPage(pageNum, pageSize, name, username);
        return Result.success(pageInfo);
    }

}
